package com.crm.service.impl.customerManager;

import com.crm.common.model.Enum.StatusCodeEnum;
import com.crm.common.util.CommonUtils;
import com.crm.dao.mapper.customMapper.customerManagerCustomMapper.CustomerCustomMapper;
import com.crm.dao.mapper.customMapper.customerManagerCustomMapper.CustomerProductDetailCustomMapper;
import com.crm.dao.mapper.customMapper.customerManagerCustomMapper.CustomerReceiveCustomMapper;
import com.crm.dao.mapper.customMapper.customerManagerCustomMapper.CustomerReturnCustomMapper;
import com.crm.dao.mapper.customMapper.opportunityManagerCustomMapper.OpportunityCustomMapper;
import com.crm.dao.mapper.customerManager.*;
import com.crm.dao.mapper.opportunityManager.OpportunityMapper;
import com.crm.model.entity.customerManager.*;
import com.crm.model.entity.sys.UserAccount;
import com.crm.model.qo.customerManager.*;
import com.crm.model.qo.opportunityManager.OpportunityQO;
import com.crm.model.vo.contactManager.IndustryAreaVO;
import com.crm.model.vo.csd.customerManager.CustomerMemberCardDetailVO;
import com.crm.model.vo.customerContractManager.CustomerChangeCourseRecordVO;
import com.crm.model.vo.customerContractManager.CustomerContractVO;
import com.crm.model.vo.customerManager.*;
import com.crm.model.vo.datasearch.DataSearchResultVO;
import com.crm.model.vo.finishOrderProduct.FinishOrderProductVO;
import com.crm.model.vo.opportunityManager.OpportunityCustomerVO;
import com.crm.redis.service.customerManager.CustomerRedisService;
import com.crm.service.customerManager.CustomerDbService;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.time.LocalDate;
import java.util.*;

/**
 * @ Author ：pym
 * @ Date   ：2021/1/18 15:56
 */
@Service
public class CustomerDbServiceImpl implements CustomerDbService {
    @Autowired
    CustomerCustomMapper customerCustomMapper;

    @Autowired
    CustomerMapper customerMapper;

    @Autowired
    CustomerCompanyInfoMapper customerCompanyInfoMapper;

    @Autowired
    DemandAnalysisMapper demandAnalysisMapper;

    @Autowired
    CustomerContactPersonMapper customerContactPersonMapper;

    @Autowired
    CompetitorMapper competitorMapper;

    @Autowired
    CustomerReturnMapper customerReturnMapper;

    @Autowired
    CustomerReceiveMapper customerReceiveMapper;

    @Autowired
    OpportunityMapper opportunityMapper;

    @Autowired
    private OpportunityCustomMapper opportunityCustomMapper;

    @Autowired
    private CustomerReturnCustomMapper customerReturnCustomMapper;

    @Autowired
    private CustomerReceiveCustomMapper customerReceiveCustomMapper;

    @Autowired
    private CustomerProductDetailMapper customerProductDetailMapper;

    @Autowired
    private CustomerProductDetailCustomMapper customerProductDetailCustomMapper;

    @Autowired
    private CustomerProductDetailMemberCardMapper customerProductDetailMemberCardMapper;

    @Autowired
    private CustomerProductDetailThroughTrainTaxMapper customerProductDetailThroughTrainTaxMapper;

    @Autowired
    private CustomerProductDetailExchangeCodeMapper customerProductDetailExchangeCodeMapper;

    @Autowired
    private CustomerProductDetailYunMapper customerProductDetailYunMapper;

    @Autowired
    private CustomerProductDetailRechargeCodeMapper customerProductDetailRechargeCodeMapper;

    @Autowired
    private CustomerProductDetailCouponMapper customerProductDetailCouponMapper;

    @Autowired
    private CustomerRedisService customerRedisService;


    @Override
    public List<CustomerVO> selectCustomerList(CustomerQO customerQO, String sortOrder, String companyId) {
        return customerCustomMapper.selectCustomerList(customerQO, sortOrder, companyId);
    }

    @Override
    public List<String> selectCustomerIdList(String customerPoolId) {
        return customerCustomMapper.selectCustomerIdList(customerPoolId);
    }

    /**
     * 客户详情
     *
     * @param customerPoolId 客户池id
     * @return
     */
    @Override
    public Customer selectCustomerByClueId(String customerPoolId) {
        return customerMapper.selectByPrimaryKey(customerPoolId);
    }

    @Override
    public Boolean batchDeleteCustomerByCustomerIds(List<String> customerIds, short deleteStatus) {
        return customerCustomMapper.batchDeleteCustomerByCustomerIds(customerIds, deleteStatus) > 0;
    }

    /**
     * 根据客户id查询需求分析
     *
     * @param order      分组类型
     * @param customerId 客户id
     * @return
     */
    @Override
    public List<DemandAnalysis> demandAnalysisList(String order, String customerId) {
        DemandAnalysisExample demandAnalysisExample = new DemandAnalysisExample();
        demandAnalysisExample.setOrderByClause(order);
        DemandAnalysisExample.Criteria criteria = demandAnalysisExample.createCriteria();
        criteria.andCustomerIdEqualTo(customerId);
        criteria.andDeleteStatusEqualTo(Short.parseShort(String.valueOf(StatusCodeEnum.NORMAL_CODE.getState())));
        List<DemandAnalysis> list = demandAnalysisMapper.selectByExample(demandAnalysisExample);
        return list;
    }

    /**
     * 客户详情-新增需求分析
     *
     * @param xq 需求分析实体类
     * @return
     */
    @Override
    public Boolean addDemandAnalysis(DemandAnalysis xq) {
        Boolean result = false;
        int num = demandAnalysisMapper.insertSelective(xq);
        if (num > 0) {
            result = true;
        }
        return result;
    }

    @Override
    public CustomerVO selectByCustomerId(String customerId) {
        return customerCustomMapper.selectByCustomerId(customerId);
    }

    @Override
    public Boolean deleteCustomerById(Customer customer) {
        return customerMapper.updateByPrimaryKeySelective(customer) > 0;
    }

    @Override
    public Boolean updateCustomerById(Customer customer) {
        return customerMapper.updateByPrimaryKeySelective(customer) > 0;
    }

    @Override
    public List<CustomerVO> selectMyCustomerList(CustomerQO customerQO, String sortOrder,
                                                 Integer dateUnit, Integer tabType, String accountId, String companyId) {
        return customerCustomMapper.selectMyCustomerList(customerQO, sortOrder, dateUnit, tabType, accountId, companyId);
    }

    /**
     * 客户详情-修改公司简介
     *
     * @param customerCompanyInfo 公司资料实体类
     * @return
     */
    @Override
    public boolean updateCustomerCompanyInfo(CustomerCompanyInfo customerCompanyInfo) {
        return customerCompanyInfoMapper.updateByPrimaryKeySelective(customerCompanyInfo) > 0;
    }

    @Override
    public Boolean ifExist(String companyName, String unifiedSocialCreditCode) {
        Boolean result = false;
        CustomerExample customerExample = new CustomerExample();
        CustomerExample.Criteria criteria = customerExample.createCriteria();
        criteria.andCustomerNameEqualTo(companyName);
        criteria.andUnifiedSocialCreditCodeEqualTo(unifiedSocialCreditCode);
        criteria.andDeleteStatusEqualTo(Short.parseShort(String.valueOf(StatusCodeEnum.NORMAL_CODE.getState())));
        List<Customer> customerList = customerMapper.selectByExample(customerExample);
        if (customerList.size() > 0) {
            result = true;
        }
        return result;
    }

    /**
     * 根据客户id查询公司联系人信息
     *
     * @param order      分组类型
     * @param customerId 客户id
     * @return
     */
    @Override
    public List<Competitor> competitorList(String order, String customerId) {
        CompetitorExample competitorExample = new CompetitorExample();
        competitorExample.setOrderByClause(order);
        CompetitorExample.Criteria criteria = competitorExample.createCriteria();
        criteria.andCustomerIdEqualTo(customerId);
        criteria.andDeleteStatusEqualTo(Short.parseShort(String.valueOf(StatusCodeEnum.NORMAL_CODE.getState())));
        List<Competitor> list = competitorMapper.selectByExample(competitorExample);
        return list;
    }

    /**
     * 新增竞品
     *
     * @param xq 竞品实体类
     * @return
     */
    @Override
    public Boolean addCompetitor(Competitor xq) {
        Boolean result = false;
        int num = competitorMapper.insertSelective(xq);
        if (num > 0) {
            result = true;
        }
        return result;
    }

    /**
     * 联系人模块 --通过登录人查询客户列表
     *
     * @param accountId 用户id
     * @return
     */
    @Override
    public List<Customer> getCustomerList(String accountId) {
        CustomerExample customerExample = new CustomerExample();
        CustomerExample.Criteria criteria = customerExample.createCriteria();
        criteria.andAccountIdEqualTo(accountId);
        criteria.andMemberErrorDataEqualTo(0);
        //criteria.andCustomerTypeEqualTo("0");
        criteria.andDeleteStatusEqualTo(Short.parseShort(String.valueOf(StatusCodeEnum.NORMAL_CODE.getState())));
        List<Customer> list = customerMapper.selectByExample(customerExample);
        return list;
    }

    @Override
    public List<Customer> getMemberCustomerList(String accountId) {
        CustomerExample customerExample = new CustomerExample();
        CustomerExample.Criteria criteria = customerExample.createCriteria();
        criteria.andAccountIdEqualTo(accountId);
        //查询会员等级大于0的客户
        criteria.andMembershipLevelGreaterThan(0);
        criteria.andDeleteStatusEqualTo(Short.parseShort(String.valueOf(StatusCodeEnum.NORMAL_CODE.getState())));
        List<Customer> list = customerMapper.selectByExample(customerExample);
        return list;
    }

    /***
     * @Description:查询非会员合同客户列表
     * @Author: luojie
     * @Date: 2021/4/23 15:06
     */
    @Override
    public List<HashMap> getNonMemberContractCustomerList(String accountId) {
        List<HashMap> result = customerCustomMapper.getNonMemberContractCustomerList(accountId);
        return result;
    }

    /**
     * 客户退回记录列表
     *
     * @param order      分组类型
     * @param customerId 客户id
     * @return
     */
    @Override
    public List<CustomerReturnVO> customerReturnList(String order, String customerId) {
        List<CustomerReturnVO> list = customerReturnCustomMapper.customerReturnList(order, customerId);
        return list;
    }

    /**
     * 客户领取记录列表
     *
     * @param order      分组类型
     * @param customerId 客户id
     * @return
     */
    @Override
    public List<CustomerReceiveVO> customerReceiveList(String order, String customerId) {
        List<CustomerReceiveVO> list = customerReceiveCustomMapper.customerReceiveList(order, customerId);
        return list;
    }

    /**
     * 新增公司资料信息
     *
     * @param customerCompanyInfon 公司资料实体
     * @return
     */
    @Override
    public boolean addCustomerCompanyInfo(CustomerCompanyInfo customerCompanyInfon) {
        Boolean result = false;
        int num = customerCompanyInfoMapper.insertSelective(customerCompanyInfon);
        if (num > 0) {
            result = true;
        }
        return result;
    }

    /**
     * 客户详情商机管理
     *
     * @param opportunityQO 客户id
     * @return
     */
    @Override
    public List<OpportunityCustomerVO> opportunityByCustomerIdList(OpportunityQO opportunityQO) {
        return opportunityCustomMapper.opportunityByCustomerIdList(opportunityQO);

    }

    /**
     * 客户详情-新增联系人-名字重复问题     客户下的联系人表
     *
     * @param customerId         客户id
     * @param contactName        联系人姓名
     * @param contactInformation 联系方式
     * @return
     */
    @Override
    public List<CustomerContactPerson> getCustomerIdAndContactName(String customerId, String contactName, String contactInformation) {
        UserAccount userAccount = (UserAccount) SecurityUtils.getSubject().getPrincipal();
        CustomerContactPersonExample customerContactPersonExample = new CustomerContactPersonExample();
        CustomerContactPersonExample.Criteria criteria = customerContactPersonExample.createCriteria();
        criteria.andCustomerIdEqualTo(customerId);
        criteria.andContactNameEqualTo(contactName);
        criteria.andContactInformationEqualTo(contactInformation);
        criteria.andCompanyIdEqualTo(userAccount.getCompanyId());
        criteria.andDeleteStatusEqualTo(Short.parseShort(String.valueOf(StatusCodeEnum.NORMAL_CODE.getState())));
        List<CustomerContactPerson> list = customerContactPersonMapper.selectByExample(customerContactPersonExample);
        return list;

    }

    /**
     * 根据用户id查询我的客户总数量
     *
     * @param accountId 用户id
     * @return
     */
    @Override
    public long selectCustomerCountByAccountId(String accountId) {
        return customerCustomMapper.selectCustomerCountByAccountId(accountId);
    }

    @Override
    public Boolean checkCustomerIsReceived(String customerId) {
        Boolean result = false;
        CustomerExample customerExample = new CustomerExample();
        CustomerExample.Criteria criteria = customerExample.createCriteria();
        criteria.andCustomerIdEqualTo(customerId);
        criteria.andCurrentStatusEqualTo(Short.parseShort("0"));
        criteria.andDeleteStatusEqualTo(Short.parseShort(String.valueOf(StatusCodeEnum.NORMAL_CODE.getState())));
        List<Customer> customerList = customerMapper.selectByExample(customerExample);
        if (customerList != null && customerList.size() > 0) {
            result = true;
        }
        return result;
    }


    @Override
    public List<CustomerRepeatVO> findRepeatCustomerName(String customerName) {
        List<CustomerRepeatVO> repeatList = customerCustomMapper.findRepeatCustomerName(customerName);

        return repeatList;
    }

    @Override
    public Customer isCustomerRepeat(String customerName, String unifiedSocialCreditCode) {
        CustomerExample customerExample = new CustomerExample();
        CustomerExample.Criteria criteria = customerExample.createCriteria();
        criteria.andCustomerNameEqualTo(customerName);
        criteria.andDeleteStatusEqualTo(Short.parseShort(String.valueOf(StatusCodeEnum.NORMAL_CODE.getState())));
        CustomerExample.Criteria criteria2 = customerExample.createCriteria();
        criteria2.andUnifiedSocialCreditCodeEqualTo(unifiedSocialCreditCode);
        criteria2.andDeleteStatusEqualTo(Short.parseShort(String.valueOf(StatusCodeEnum.NORMAL_CODE.getState())));
        customerExample.or(criteria2);
        List<Customer> customerList = customerMapper.selectByExample(customerExample);
        if (customerList != null && customerList.size() > 0) {
            return customerList.get(0);
        }
        return null;
    }

    @Override
    public long selectCustomerCount(String customerPoolId) {
        CustomerExample customerExample = new CustomerExample();
        CustomerExample.Criteria criteria = customerExample.createCriteria();
        criteria.andCustomerPoolIdEqualTo(customerPoolId);
        criteria.andDeleteStatusEqualTo(Short.parseShort(String.valueOf(StatusCodeEnum.NORMAL_CODE.getState())));
        long count = customerMapper.countByExample(customerExample);
        return count;
    }

    @Override
    public Boolean insertCustomer(Customer customer) {
        Boolean result = false;
        int num = customerMapper.insertSelective(customer);
        if (num > 0) {
            result = true;
        }
        return result;
    }

    /**
     * 去重 查询客户是否一样 通过客户名称和统一社会信用码查询有没有这个客户
     *
     * @param customerName            客户名称
     * @param unifiedSocialCreditCode 统一社会信用码
     * @param accountId               登录人id
     * @return
     */
    @Override
    public Boolean customerIdAndUnifiedSocialCreditCode(String customerName, String unifiedSocialCreditCode, String accountId) {
        Boolean result = false;
        CustomerExample customerExample = new CustomerExample();
        CustomerExample.Criteria criteria = customerExample.createCriteria();
        criteria.andCustomerNameEqualTo(customerName);
        criteria.andUnifiedSocialCreditCodeEqualTo(unifiedSocialCreditCode);
        criteria.andAccountIdEqualTo(accountId);
        criteria.andDeleteStatusEqualTo(Short.parseShort(String.valueOf(StatusCodeEnum.NORMAL_CODE.getState())));
        List<Customer> customerList = customerMapper.selectByExample(customerExample);
        if (customerList.size() == 0) {
            result = true;
        }
        return result;
    }

    /**
     * 一个用户下最多200个客户
     *
     * @param accountId 用户id
     * @return
     */
    @Override
    public List<Customer> selectCustomerSize(String accountId) {
        CustomerExample customerExample = new CustomerExample();
        CustomerExample.Criteria criteria = customerExample.createCriteria();
        criteria.andCreatePeopleIdEqualTo(accountId);
        criteria.andDeleteStatusEqualTo(Short.parseShort(String.valueOf(StatusCodeEnum.NORMAL_CODE.getState())));
        List<Customer> customerList = customerMapper.selectByExample(customerExample);
        return customerList;
    }

    /**
     * 查询客户信息
     *
     * @param customerId 客户id
     * @return
     */
    @Override
    public Customer selectCustomerById(String customerId) {
        return customerMapper.selectByPrimaryKey(customerId);
    }

    /**
     * 修改客户
     *
     * @param customer 客户实体类
     * @return
     */
    @Override
    public Boolean updateCustomerByPrimaryKey(Customer customer) {
        return customerMapper.updateByPrimaryKeyWithBLOBs(customer) > 0;
    }

    @Override
    public List<Customer> getCustomerListByComp(String companyId) {
        return customerCustomMapper.getCustomerListByComp(companyId);
    }


    /**
     * 移交资料页面获取客户列表
     *
     * @param moveCustomerQO
     * @param toString
     * @return
     * @Author renshaorong
     * @Date 2021/4/9
     */
    @Override
    public List<CustomerVO> getCustomerListByUserAccountId(MoveCustomerQO moveCustomerQO, String toString) {
        return customerCustomMapper.getCustomerListByUserAccountId(moveCustomerQO, toString);
    }

    /**
     * 批量移动客户给指定用户
     *
     * @param customerList    被选中客户List
     * @param updateAccountId 更新者
     * @return
     * @Author renshaorong
     * @Date 2021/4/9
     */
    @Override
    public boolean moveSelectedCustomers(List<Customer> customerList, String updateAccountId) {
        return customerCustomMapper.updateAccountId(customerList, updateAccountId) > 0;
    }

    /**
     * 统计客户
     *
     * @param qo
     * @return
     */
    @Override
    public AnalyseCustomerVO analyseCustomer(AnalyseCustomerQO qo) {
        return customerCustomMapper.analyseCustomer(qo);
    }

    /**
     * 查询指定时间范围新客户
     *
     * @param qo
     * @return
     */
    @Override
    public List<Long> selectAnalyseAddCustomer(AnalyseCustomerQO qo) {
        return customerCustomMapper.selectAnalyseAddCustomer(qo);
    }

    /**
     * 成交客户数量
     *
     * @param qo
     * @return
     */
    @Override
    public List<Long> selectMakeCustomerCount(AnalyseCustomerQO qo) {
        return customerCustomMapper.selectMakeCustomerCount(qo);
    }

    /**
     * 查询会员成交产品
     *
     * @param qo
     * @return
     */
    @Override
    public BigDecimal selectCustomerVipProductMackCount(AnalyseCustomerQO qo) {
        return customerCustomMapper.selectCustomerVipProductMackCount(qo);
    }

    /**
     * 查询非会员成交产品
     *
     * @param qo
     * @return
     */
    @Override
    public BigDecimal selectCustomerProductMackCount(AnalyseCustomerQO qo) {
        return customerCustomMapper.selectCustomerProductMackCount(qo);
    }

    /**
     * 到期会员
     *
     * @param qo
     * @return
     */
    @Override
    public BigDecimal selectExpireVip(AnalyseCustomerQO qo) {
        return customerCustomMapper.selectExpireVip(qo);
    }

    /**
     * 首页快速查重
     *
     * @param type         类型：1-按客户名称查重；2-按联系方式查重; 3-按关键字查重
     * @param isVip        是否会员
     * @param checkContent 文本内容
     * @return
     * @author yuanyunfeng
     * @date 2021/4/19 14:46
     */
    @Override
    public List<CustomerFQRepeatVO> fastQueryRepeat(String type, String isVip, String checkContent) {
        if (StringUtils.isNotBlank(type) && StringUtils.isNotBlank(isVip)) {
            if ("3".equals(type)) {
                //选择类型（3-关键字）+是否会员产品条件时
                return customerCustomMapper.fastQueryRepeat(type, isVip, checkContent);
            } else if ("1".equals(type) || "4".equals(type)) {
                return customerCustomMapper.fastQueryRepeatAndRelation(type, isVip, checkContent);
            }
        }
        //根据手机号查重
        return customerCustomMapper.fastQueryRepeatByPhone(type, checkContent);
    }


    /**
     * 首页快速查重（内训）
     *
     * @param type         类型：1-按客户名称查重；2-按联系方式查重; 3-按关键字查重
     * @param isVip        是否会员
     * @param checkContent 文本内容
     * @return
     * @author yuanyunfeng
     * @date 2021/4/19 14:46
     */
    @Override
    public List<CustomerFQRepeatVO> fastQueryRepeatByNx(String type, String isVip, String checkContent) {
        if (StringUtils.isNotBlank(type) && StringUtils.isNotBlank(isVip)) {
            if ("3".equals(type)) {
                //选择类型（3-关键字）+是否会员产品条件时
                return customerCustomMapper.fastQueryRepeatByNx(type, isVip, checkContent);
            } else if ("1".equals(type) || "4".equals(type)) {
                return customerCustomMapper.fastQueryRepeatByNxAndRelation(type, isVip, checkContent);
            }
        }
        return null;
    }

    /**
     * 获取一个客户
     *
     * @return
     */
    @Override
    public CustomerVO selectOneCustomer() {
        return customerCustomMapper.selectOneCustomer();
    }

    /**
     * 判断个人客户是否存在
     *
     * @param enterpriseExternalPhone
     * @return {@link {java.lang.Boolean}}
     * @Author pym
     * @Date 2021/4/19 16:49
     */
    @Override
    public Boolean isIndividualCustomerRepeat(String enterpriseExternalPhone) {
        Boolean result = false;
        CustomerExample customerExample = new CustomerExample();
        CustomerExample.Criteria criteria = customerExample.createCriteria();
        criteria.andContactPersonPhoneEqualTo(enterpriseExternalPhone);
        criteria.andCustomerTypeEqualTo("1");
        criteria.andDeleteStatusEqualTo(Short.parseShort(String.valueOf(StatusCodeEnum.NORMAL_CODE.getState())));
        List<Customer> customerList = customerMapper.selectByExample(customerExample);
        if (customerList.size() > 0) {
            result = true;
        }
        return result;
    }

    /**
     * 会员保持率
     *
     * @param year      年份
     * @param companyId
     * @return
     */
    @Override
    public List<Integer> getAnalyseVipHoldRatio(Integer year, String companyId) {
        return customerCustomMapper.getAnalyseVipHoldRatio(year, companyId);
    }

    /**
     * 会员流失率
     *
     * @param year      年份
     * @param companyId
     * @return
     */
    @Override
    public List<Integer> getAnalyseVipOutRatio(Integer year, String companyId) {
        return customerCustomMapper.getAnalyseVipOutRatio(year, companyId);
    }

    /**
     * 会员客户数量统计
     *
     * @param startDate
     * @param companyId
     * @return
     */
    @Override
    public VipCustomerAnalyseVO getVipCustomerCount(LocalDate startDate, String companyId) {
        return customerCustomMapper.getVipCustomerCount(startDate, companyId);
    }

    /**
     * 会员客户列表
     *
     * @param membershipLevel
     * @param order
     * @return
     */
    @Override
    public List<VipCustomerListVO> selectVipCustomerList(Integer membershipLevel, String order, String companyId) {
        return customerCustomMapper.selectVipCustomerList(membershipLevel, order, companyId);
    }

    /**
     * 联系人列表跳转客户详情-查询是不是自己的客户
     *
     * @param customerId 客户id
     * @return
     */
    @Override
    public boolean selectCustomerSelf(String customerId, String accountId) {
        UserAccount userAccount = (UserAccount) SecurityUtils.getSubject().getPrincipal();
        boolean rs = CommonUtils.isAdmin(userAccount.getUserId());
        if (rs) {
            return true;
        }


        CustomerExample customerExample = new CustomerExample();
        CustomerExample.Criteria criteria = customerExample.createCriteria();
        criteria.andCustomerIdEqualTo(customerId);
        criteria.andAccountIdEqualTo(accountId);
        List<Customer> customerList = customerMapper.selectByExample(customerExample);
        return customerList.size() != 0;
    }

    /**
     * 新增商机-客户名称精准匹配
     *
     * @param customerName 客户名称
     * @return
     */
    @Override
    public boolean selectCustomerNameReg(String customerName) {
        CustomerExample customerExample = new CustomerExample();
        CustomerExample.Criteria criteria = customerExample.createCriteria();
        criteria.andCustomerNameEqualTo(customerName);
        List<Customer> customerList = customerMapper.selectByExample(customerExample);
        return customerList.size() != 0;
    }

    /**
     * 客户管理 - 单品客户 - 新增单品客户列表
     *
     * @param customerName 客户名称
     * @return {@link {java.util.List<com.crm.model.vo.customerManager.CustomerVO>}}
     * @Author pym
     * @Date 2021/4/30 10:58
     */
    @Override
    public List<CustomerVO> selectSingleProductCustomerList(String customerName, String accountId, String companyId) {
        return customerCustomMapper.selectSingleProductCustomerList(customerName, accountId, companyId);
    }

    /**
     * qo精确查询客户
     *
     * @param customerQO
     * @return
     */
    @Override
    public List<CustomerVO> selectCustomerPreciseSearch(CustomerQO customerQO) {
        return customerCustomMapper.selectCustomerPreciseSearch(customerQO);
    }

    /**
     * 获取最大的客户编号
     *
     * @return
     */
    @Override
    public String selectMaxCustomerNumber(String datetime) {
        return customerCustomMapper.selectMaxCustomerNumber(datetime);
    }


    /**
     * 根据客户名称检索数据
     *
     * @param customerName 客户名称
     * @return
     */
    @Override
    public List<DataSearchResultVO> selectDataByCustomerName(String customerName) {
        return customerCustomMapper.selectDataByCustomerName(customerName);
    }

    /**
     * 客户管理-客户详情-会员产品-详细信息
     *
     * @param customerId            客户id
     * @param finishOrderProductNum 产品号
     * @return
     */
    @Override
    public List<CourseVO> selectCustomerProductDetail(String customerId, String finishOrderProductNum) {
        List<CourseVO> list = customerProductDetailCustomMapper.selectCustomerProductDetail(customerId, finishOrderProductNum);
        return list;
    }

    /**
     * 新增会员产品详情
     *
     * @param customerProductDetail 会员产品详情
     */
    @Override
    public boolean insertCustomerProductDetail(CustomerProductDetail customerProductDetail) {
        Boolean result = false;
        int num = customerProductDetailMapper.insertSelective(customerProductDetail);
        if (num > 0) {
            result = true;
        }
        return result;
    }

    /**
     * 客户管理-客户详情-会员产品-课程-产品信息
     *
     * @param customerId 客户Id
     * @param courseNum  课程标号
     * @return
     */
    @Override
    public List<CustomerProductVO> selectCustomerProduct(String customerId, String courseNum) {
        return customerProductDetailCustomMapper.selectCustomerProduct(customerId, courseNum);
    }

    /**
     * 我的客户-查询客户里面有的行业
     *
     * @param name   登录人
     * @param number 0超管 1普通管理员  2普通用户
     * @return
     */
    @Override
    public List<IndustryAreaVO> selectIndustry(String name, Integer number) {
        return customerCustomMapper.selectIndustry(name, number);
    }

    /**
     * 我的客户-查询客户里面有的地区
     *
     * @param name   登录人
     * @param number 0超管 1普通管理员  2普通用户
     * @return
     */
    @Override
    public List<IndustryAreaVO> selectProvince(String name, Integer number) {
        return customerCustomMapper.selectProvince(name, number);
    }

    /**
     * 我的客户-查询客户里面的热门标签
     *
     * @param name   登录人
     * @param number 0超管 1普通管理员  2普通用户
     */
    @Override
    public List<TagVO> selectTag(String name, Integer number) {
        return customerCustomMapper.selectTag(name, number);
    }

    /**
     * 查询当前登录人各种状态客户数量
     *
     * @param accountId
     * @return
     */
    @Override
    public AnalyseCustomerRedisVO getAnalyseCustomerIntention(String accountId, Integer dateUnit) {
        return customerCustomMapper.getAnalyseCustomerIntention(accountId, dateUnit);
    }

    /**
     * 查询指定时间段的客户数量
     *
     * @param qo
     * @return
     */
    @Override
    public Long selectCustomerCountByAccountIdAndDate(AnalyseCustomerQO qo) {
        return customerCustomMapper.selectCustomerCountByAccountIdAndDate(qo);
    }

    /**
     * 统计成交客户数量
     *
     * @param qo
     * @return
     */
    @Override
    public Long dealCustomerCount(AnalyseCustomerQO qo) {
        return customerCustomMapper.dealCustomerCount(qo);
    }

    /**
     * 客户-客户订单完成的会员产品会员卡
     *
     * @param customerProductDetailMemberCard 客户订单完成的会员产品会员卡实体
     * @return
     */
    @Override
    public Boolean addCustomerProductDetailMemberCard(CustomerProductDetailMemberCard customerProductDetailMemberCard) {
        Boolean result = false;
        int num = customerProductDetailMemberCardMapper.insertSelective(customerProductDetailMemberCard);
        if (num > 0) {
            result = true;
        }
        return result;
    }

    /**
     * 通过会员卡id查询会员卡信息
     *
     * @param customerProductDetailMemberCardId 会员卡id
     * @return
     */
    @Override
    public CustomerProductDetailMemberCard getCustomerProductDetailMemberCardById(String customerProductDetailMemberCardId) {
        return customerProductDetailMemberCardMapper.selectByPrimaryKey(customerProductDetailMemberCardId);
    }

    /**
     * 根据客户名称和账号查询客户
     *
     * @param customerName 客户名称
     * @param accountId    账号ID
     * @return {@link {com.crm.model.entity.customerManager.Customer}}
     * @Author pym
     * @Date 2021/6/1 18:08
     */
    @Override
    public Customer selectCustomerByNameAndId(String customerName, String accountId) {
        CustomerExample customerExample = new CustomerExample();
        CustomerExample.Criteria criteria = customerExample.createCriteria();
        criteria.andCustomerNameEqualTo(customerName);
        criteria.andAccountIdEqualTo(accountId);
        criteria.andDeleteStatusEqualTo(Short.parseShort(String.valueOf(StatusCodeEnum.NORMAL_CODE.getState())));
        List<Customer> customerList = customerMapper.selectByExample(customerExample);
        if (customerList != null && customerList.size() > 0) {
            return customerList.get(0);
        }
        return null;
    }

    /**
     * 通过客户id和产品号查询会员卡信息
     *
     * @param customerId            客户id
     * @param finishOrderProductNum 会员卡id
     * @return
     */
    @Override
    public CustomerProductDetailMemberCard getCustomerProductDetailMemberCard(String customerId, String finishOrderProductNum) {
        CustomerProductDetailMemberCardExample customerProductDetailMemberCardExample = new CustomerProductDetailMemberCardExample();
        CustomerProductDetailMemberCardExample.Criteria criteria = customerProductDetailMemberCardExample.createCriteria();
        criteria.andCustomerIdEqualTo(customerId);
        criteria.andFinishOrderProductNumEqualTo(finishOrderProductNum);
        List<CustomerProductDetailMemberCard> list = customerProductDetailMemberCardMapper.selectByExample(customerProductDetailMemberCardExample);
        if (list != null && list.size() > 0) {
            return list.get(0);
        } else {
            return new CustomerProductDetailMemberCard();
        }


    }

    /**
     * 根据用户id查询客户跟进标签前十
     *
     * @param accountId
     * @return
     */
    @Override
    public List<String> selectCustomerFollowTag(String accountId) {
        return customerCustomMapper.selectCustomerFollowTag(accountId);
    }

    /**
     * 联系人-新增个人联系人--查询同一用户下的联系方式不能一样
     *
     * @param contactName        客户名称
     * @param contactInformation 联系方式
     * @param contactNature      联系人性质  0企业联系人  1非企业联系人
     * @param accountId          用户id
     * @return
     */
    @Override
    public Customer getCustomerNameAndPhone(String contactName, String contactInformation, String contactNature, String accountId) {
        CustomerExample customerExample = new CustomerExample();
        CustomerExample.Criteria criteria = customerExample.createCriteria();
        criteria.andCustomerNameEqualTo(contactName);
        criteria.andCustomerTypeEqualTo(contactNature);
        criteria.andContactPersonPhoneEqualTo(contactInformation);
        criteria.andAccountIdEqualTo(accountId);
        criteria.andDeleteStatusEqualTo(Short.parseShort(String.valueOf(StatusCodeEnum.NORMAL_CODE.getState())));
        List<Customer> list = customerMapper.selectByExample(customerExample);
        if (list != null && list.size() > 0) {
            return list.get(0);
        } else {
            return new Customer();
        }
    }

    /**
     * 通过客户名称和用户id查询这个用户下有这个客户吗
     *
     * @param accountId    用户id
     * @param customerName 客户名称
     * @return
     */
    @Override
    public Customer selectCustomerNameAndAccountId(String accountId, String customerName) {
        CustomerExample customerExample = new CustomerExample();
        CustomerExample.Criteria criteria = customerExample.createCriteria();
        criteria.andCustomerNameEqualTo(customerName);
        criteria.andAccountIdEqualTo(accountId);
        criteria.andDeleteStatusEqualTo(Short.parseShort(String.valueOf(StatusCodeEnum.NORMAL_CODE.getState())));
        List<Customer> list = customerMapper.selectByExample(customerExample);
        if (list != null && list.size() > 0) {
            return list.get(0);
        } else {
            return new Customer();
        }
    }

    /**
     * 根据客户名称查询
     *
     * @param customerName
     * @return
     */
    @Override
    public CustomerVO selectCustomerByName(String customerName) {
        return customerCustomMapper.selectCustomerByName(customerName);
    }

    /**
     * 新增会员卡下的税法直通车数据
     *
     * @param: [customerProductDetailThroughTrainTax]
     * @return: java.lang.Boolean
     * @Author: baijian
     * @Date: 2021/6/21
     */
    @Override
    public Boolean addCustomerProductDetailThroughTrainTax(CustomerProductDetailThroughTrainTax customerProductDetailThroughTrainTax) {
        Boolean result = false;
        int num = customerProductDetailThroughTrainTaxMapper.insertSelective(customerProductDetailThroughTrainTax);
        if (num > 0) {
            result = true;
        }
        return result;
    }

    /**
     * 根据会员卡查询下面的税法直通车数据
     *
     * @param customerProductDetailMemberCardId 税法直通车
     * @return
     */
    @Override
    public List<CustomerProductDetailThroughTrainTax> customerProductDetailThroughTrainTaxList(String customerProductDetailMemberCardId) {
        CustomerProductDetailThroughTrainTaxExample customerProductDetailThroughTrainTaxExample = new CustomerProductDetailThroughTrainTaxExample();
        CustomerProductDetailThroughTrainTaxExample.Criteria criteria = customerProductDetailThroughTrainTaxExample.createCriteria();
        criteria.andCustomerProductDetailMemberCardIdEqualTo(customerProductDetailMemberCardId);
        List<CustomerProductDetailThroughTrainTax> list = customerProductDetailThroughTrainTaxMapper.selectByExample(customerProductDetailThroughTrainTaxExample);
        return list;
    }

    /**
     * 根据客户名称和统一社会信用代码查询客户信息
     *
     * @param customerName            客户名称
     * @param unifiedSocialCreditCode 统一社会信用代码
     * @return {@link {com.crm.model.entity.customerManager.Customer}}
     * @Author pym
     * @Date 2021/7/1 13:36
     */
    @Override
    public Customer selectCustomerByNameAndCode(String customerName, String unifiedSocialCreditCode) {
        CustomerExample customerExample = new CustomerExample();
        CustomerExample.Criteria criteria = customerExample.createCriteria();
        criteria.andCustomerNameEqualTo(customerName);
        criteria.andUnifiedSocialCreditCodeEqualTo(unifiedSocialCreditCode);
        criteria.andDeleteStatusEqualTo(Short.parseShort(String.valueOf(StatusCodeEnum.NORMAL_CODE.getState())));
        List<Customer> customerList = customerMapper.selectByExample(customerExample);
        if (customerList != null && customerList.size() > 0) {
            return customerList.get(0);
        }
        return null;
    }

    /**
     * 去Redis判断客户名称是否存在
     *
     * @param customerName 客户名称
     * @return {@link {java.lang.Boolean}}
     * @Author pym
     * @Date 2021/7/1 14:25
     */
    @Override
    public Boolean isCustomerExist(String customerName) {
        return customerRedisService.isCustomerExist(customerName);
    }

    /**
     * 根据客户名称去Redis查统一社会信用代码
     *
     * @param customerName 客户名称
     * @return {@link {java.lang.String}}
     * @Author pym
     * @Date 2021/7/1 14:46
     */
    @Override
    public String selectUnifiedSocialCreditCode(String customerName) {
        return customerRedisService.selectUnifiedSocialCreditCode(customerName);
    }

    /**
     * 根据客户名称和联系人电话查询客户信息
     *
     * @param customerName       客户名称
     * @param contactPersonPhone 联系人电话
     * @return {@link {com.crm.model.entity.customerManager.Customer}}
     * @Author pym
     * @Date 2021/7/2 11:22
     */
    @Override
    public Customer selectCustomerByNameAndPhone(String customerName, String contactPersonPhone) {
        CustomerExample customerExample = new CustomerExample();
        CustomerExample.Criteria criteria = customerExample.createCriteria();
        criteria.andCustomerNameEqualTo(customerName);
        criteria.andContactPersonPhoneEqualTo(contactPersonPhone);
        criteria.andCustomerTypeEqualTo("1");
        criteria.andDeleteStatusEqualTo(Short.parseShort(String.valueOf(StatusCodeEnum.NORMAL_CODE.getState())));
        List<Customer> customerList = customerMapper.selectByExample(customerExample);
        if (customerList != null && customerList.size() > 0) {
            return customerList.get(0);
        }
        return null;
    }

    /**
     * 个人客户去重
     *
     * @param customerName 客户姓名
     * @param accountId    用户id
     * @param customerType 客户类型  0企业客户 1个人客户
     * @return
     */
    @Override
    public CustomerVO selectCustomerByNameAndAccountId(String customerName, String accountId, String customerType, String companyId) {
        return customerCustomMapper.selectCustomerByNameAndAccountId(customerName, accountId, customerType, companyId);
    }

    /**
     * 更新给定字段，字段为空时不更新
     *
     * @param customer
     * @return
     * @Author renshaorong
     * @Date 2021/7/12
     */
    @Override
    public boolean updateByPrimaryKeySelective(Customer customer) {
        return customerMapper.updateByPrimaryKeySelective(customer) > 0;
    }

    /**
     * 信息错误页面
     *
     * @param customerQO
     * @param sortOrder
     * @return
     */
    @Override
    public List<CustomerVO> selectInfomationErrorList(CustomerQO customerQO, String sortOrder) {
        return customerCustomMapper.selectInfomationErrorList(customerQO, sortOrder);
    }

    /**
     * 获取会员客户数量
     *
     * @param year
     * @param companyId
     * @return
     */
    @Override
    public List<Integer> getCustomerCount(Integer year, String companyId) {
        return customerCustomMapper.getCustomerCount(year, companyId);
    }

    /**
     * 通过用户id查询企业客户
     *
     * @param accountId 用户id
     * @Author renshaorong
     * @Date 2021/7/13
     */
    @Override
    public List<Customer> getCompCustomerList(String accountId, String order) {
        CustomerExample customerExample = new CustomerExample();
        CustomerExample.Criteria criteria = customerExample.createCriteria();
        criteria.andAccountIdEqualTo(accountId);
        criteria.andCustomerTypeEqualTo("0");
        criteria.andDeleteStatusEqualTo(Short.parseShort(String.valueOf(StatusCodeEnum.NORMAL_CODE.getState())));
        customerExample.setOrderByClause(order);
        List<Customer> list = customerMapper.selectByExample(customerExample);
        return list;
    }

    /**
     * 根据统一社会信用代码查询客户信息
     *
     * @param unifiedSocialCreditCode 统一社会信用代码
     * @return {@link {com.crm.model.entity.customerManager.Customer}}
     * @Author pym
     * @Date 2021/7/1 13:36
     */
    @Override
    public Customer selectCustomerByCode(String unifiedSocialCreditCode) {
        CustomerExample customerExample = new CustomerExample();
        CustomerExample.Criteria criteria = customerExample.createCriteria();
        criteria.andUnifiedSocialCreditCodeEqualTo(unifiedSocialCreditCode);
        criteria.andDeleteStatusEqualTo(Short.parseShort(String.valueOf(StatusCodeEnum.NORMAL_CODE.getState())));
        List<Customer> customerList = customerMapper.selectByExample(customerExample);
        if (customerList != null && customerList.size() > 0) {
            return customerList.get(0);
        }
        return null;
    }

    /**
     * 查询我的客户非会员客户数量
     *
     * @param accountId 账号Id
     * @return {@link {long}}
     * @Author pym
     * @Date 2021/7/20 16:04
     */
    @Override
    public long selectNonmemberCustomerCountByAccountId(String accountId) {
        return customerCustomMapper.selectNonmemberCustomerCountByAccountId(accountId);
    }

    @Override
    public Map<String, Object> getPoolType(String customerId) {
        return customerCustomMapper.getPoolType(customerId);
    }

    /**
     * 新增兑换码
     *
     * @param ex 兑换码
     * @return
     */
    @Override
    public Boolean addCustomerProductDetailExchangeCode(CustomerProductDetailExchangeCode ex) {
        Boolean result = false;
        int num = customerProductDetailExchangeCodeMapper.insertSelective(ex);
        if (num > 0) {
            result = true;
        }
        return result;
    }

    /**
     * 新增云顾问卡
     *
     * @param yun 云顾问卡
     * @return
     */
    @Override
    public Boolean addCustomerProductDetailYun(CustomerProductDetailYun yun) {
        Boolean result = false;
        int num = customerProductDetailYunMapper.insertSelective(yun);
        if (num > 0) {
            result = true;
        }
        return result;
    }

    /**
     * 根据订单id和产品编码查询会员卡信息
     *
     * @param orderId               订单id
     * @param finishOrderProductNum 产品号
     * @return
     */
    @Override
    public List<CustomerProductDetailMemberCard> getMemberCardByOrderIdAndCph(String orderId, String finishOrderProductNum) {
        CustomerProductDetailMemberCardExample customerProductDetailMemberCardExample = new CustomerProductDetailMemberCardExample();
        CustomerProductDetailMemberCardExample.Criteria criteria = customerProductDetailMemberCardExample.createCriteria();
        customerProductDetailMemberCardExample.setOrderByClause("create_tm asc");
        criteria.andProductOrderIdEqualTo(orderId);
        if (!finishOrderProductNum.contains("CCC")) {
            criteria.andFinishOrderProductNumEqualTo(finishOrderProductNum);
            criteria.andDeleteStatusEqualTo(Short.parseShort(String.valueOf(StatusCodeEnum.NORMAL_CODE.getState())));
        } else {
            finishOrderProductNum = finishOrderProductNum.substring(0, finishOrderProductNum.length() - 3);
            criteria.andFinishOrderProductNumEqualTo(finishOrderProductNum);
        }
        List<CustomerProductDetailMemberCard> list = customerProductDetailMemberCardMapper.selectByExample(customerProductDetailMemberCardExample);
        return list;
    }

    /**
     * 根据订单id和产品编码查询税法直通车信息
     *
     * @param orderId               订单id
     * @param finishOrderProductNum 产品号
     * @return
     */
    @Override
    public List<CustomerProductDetailThroughTrainTax> getThroughTrainTaxByOrderIdAndCph(String orderId, String finishOrderProductNum) {
        CustomerProductDetailThroughTrainTaxExample customerProductDetailThroughTrainTaxExample = new CustomerProductDetailThroughTrainTaxExample();
        CustomerProductDetailThroughTrainTaxExample.Criteria criteria = customerProductDetailThroughTrainTaxExample.createCriteria();
        customerProductDetailThroughTrainTaxExample.setOrderByClause("create_tm asc");
        criteria.andProductOrderIdEqualTo(orderId);
        if (!finishOrderProductNum.contains("CCC")) {
            criteria.andFinishOrderProductNumEqualTo(finishOrderProductNum);
            criteria.andDeleteStatusEqualTo(Short.parseShort(String.valueOf(StatusCodeEnum.NORMAL_CODE.getState())));
        } else {
            finishOrderProductNum = finishOrderProductNum.substring(0, finishOrderProductNum.length() - 3);
            criteria.andFinishOrderProductNumEqualTo(finishOrderProductNum);
        }
        List<CustomerProductDetailThroughTrainTax> list = customerProductDetailThroughTrainTaxMapper.selectByExample(customerProductDetailThroughTrainTaxExample);
        return list;
    }

    /**
     * 根据订单id和产品编码查询兑换码信息
     *
     * @param orderId               订单id
     * @param finishOrderProductNum 产品号
     * @return
     */
    @Override
    public List<CustomerProductDetailExchangeCode> getExchangeCodeByOrderIdAndCph(String orderId, String finishOrderProductNum) {
        CustomerProductDetailExchangeCodeExample customerProductDetailExchangeCodeExample = new CustomerProductDetailExchangeCodeExample();
        CustomerProductDetailExchangeCodeExample.Criteria criteria = customerProductDetailExchangeCodeExample.createCriteria();
        customerProductDetailExchangeCodeExample.setOrderByClause("create_tm asc");
        criteria.andProductOrderIdEqualTo(orderId);
        if (!"1".equals(finishOrderProductNum)) {
            if (!finishOrderProductNum.contains("CCC")) {
                criteria.andFinishOrderProductNumEqualTo(finishOrderProductNum);
                criteria.andDeleteStatusEqualTo(Short.parseShort(String.valueOf(StatusCodeEnum.NORMAL_CODE.getState())));
            } else {
                finishOrderProductNum = finishOrderProductNum.substring(0, finishOrderProductNum.length() - 3);
                criteria.andFinishOrderProductNumEqualTo(finishOrderProductNum);
            }
        } else {
            criteria.andDeleteStatusEqualTo(Short.parseShort(String.valueOf(StatusCodeEnum.NORMAL_CODE.getState())));
        }

        List<CustomerProductDetailExchangeCode> list = customerProductDetailExchangeCodeMapper.selectByExample(customerProductDetailExchangeCodeExample);
        return list;
    }

    /**
     * 根据订单id和产品编码查询云顾问卡信息
     *
     * @param orderId               订单id
     * @param finishOrderProductNum 产品号
     * @return
     */
    @Override
    public List<CustomerProductDetailYun> getYunByOrderIdAndCph(String orderId, String finishOrderProductNum) {
        CustomerProductDetailYunExample customerProductDetailYunExample = new CustomerProductDetailYunExample();
        CustomerProductDetailYunExample.Criteria criteria = customerProductDetailYunExample.createCriteria();
        customerProductDetailYunExample.setOrderByClause("create_tm asc");
        criteria.andProductOrderIdEqualTo(orderId);
        if (!finishOrderProductNum.contains("CCC")) {
            criteria.andFinishOrderProductNumEqualTo(finishOrderProductNum);
            criteria.andDeleteStatusEqualTo(Short.parseShort(String.valueOf(StatusCodeEnum.NORMAL_CODE.getState())));
        } else {
            finishOrderProductNum = finishOrderProductNum.substring(0, finishOrderProductNum.length() - 3);
            criteria.andFinishOrderProductNumEqualTo(finishOrderProductNum);
        }
        List<CustomerProductDetailYun> list = customerProductDetailYunMapper.selectByExample(customerProductDetailYunExample);
        return list;
    }

    /**
     * 查询当月流失会员数量
     *
     * @param month
     * @param companyId
     * @return
     */
    @Override
    public Integer selectThisMonthOutVip(String month, String companyId) {
        return customerCustomMapper.selectThisMonthOutVip(month, companyId);
    }

    /**
     * 当月会员数量
     *
     * @param month
     * @param companyId
     * @return
     */
    @Override
    public Integer selectThisMonthVipCount(String month, String companyId) {
        return customerCustomMapper.selectThisMonthVipCount(month, companyId);
    }

    /**
     * 查询客户名称和统一社会信用代码不为空的客户
     *
     * @param
     * @return {@link {java.util.List<com.crm.model.entity.customerManager.Customer>}}
     * @Author pym
     * @Date 2021/7/21 13:31
     */
    @Override
    public List<Customer> getAllCustomerWhereNameAndCreditCodeNotNull() {
        CustomerExample customerExample = new CustomerExample();
        CustomerExample.Criteria criteria = customerExample.createCriteria();
        criteria.andCustomerNameIsNotNull();
        criteria.andCustomerNameNotEqualTo("");
        criteria.andUnifiedSocialCreditCodeIsNotNull();
        criteria.andUnifiedSocialCreditCodeNotEqualTo("");
        criteria.andDeleteStatusEqualTo(Short.parseShort(String.valueOf(StatusCodeEnum.NORMAL_CODE.getState())));
        return customerMapper.selectByExample(customerExample);
    }

    /**
     * 先查Redis判断客户是否存在
     *
     * @param customerName            客户名称
     * @param unifiedSocialCreditCode 统一社会信用代码
     * @return {@link {java.lang.Boolean}}
     * @Author pym
     * @Date 2021/8/3 14:53
     */
    @Override
    public Boolean selectCustomerIfRepeated(String customerName, String unifiedSocialCreditCode) {
        Boolean result = false;
        //先在redis中查询客户信息，若没有则在数据库中查询，再插入到redis中
        Customer customer = customerRedisService.getCustomerByNameAndCode(customerName, unifiedSocialCreditCode);
        if (customer == null) {
            Customer customerRepeat = isCustomerRepeat(customerName, unifiedSocialCreditCode);
            if (customerRepeat != null) {
                result = true;
                if (customerRepeat.getCustomerName().equals(customerName)
                        && customerRepeat.getUnifiedSocialCreditCode().equals(unifiedSocialCreditCode)) {
                    customerRedisService.setCustomerByNameAndCode(customerRepeat);
                }
            } else {
                result = false;
            }
        } else {
            result = true;
        }
        return result;
    }

    /**
     * 新增充值卡
     *
     * @param ex 充值卡
     * @return
     */
    @Override
    public Boolean addCustomerProductDetailRechargeCode(CustomerProductDetailRechargeCode ex) {
        Boolean result = false;
        int num = customerProductDetailRechargeCodeMapper.insertSelective(ex);
        if (num > 0) {
            result = true;
        }
        return result;
    }

    /**
     * 新增优惠劵
     *
     * @param c 优惠劵
     */
    @Override
    public Boolean addCustomerProductDetailCoupon(CustomerProductDetailCoupon c) {
        Boolean result = false;
        int num = customerProductDetailCouponMapper.insertSelective(c);
        if (num > 0) {
            result = true;
        }
        return result;
    }

    /**
     * 查询充值卡通过订单id和产品号
     *
     * @param orderId               订单id
     * @param finishOrderProductNum 产品号
     * @return
     */
    @Override
    public List<CustomerProductDetailRechargeCode> getRechargeCodeByOrderIdAndCph(String orderId, String finishOrderProductNum) {
        CustomerProductDetailRechargeCodeExample customerProductDetailRechargeCodeExample = new CustomerProductDetailRechargeCodeExample();
        CustomerProductDetailRechargeCodeExample.Criteria criteria = customerProductDetailRechargeCodeExample.createCriteria();
        customerProductDetailRechargeCodeExample.setOrderByClause("create_tm asc");
        criteria.andProductOrderIdEqualTo(orderId);
        if (!finishOrderProductNum.contains("CCC")) {
            criteria.andFinishOrderProductNumEqualTo(finishOrderProductNum);
            criteria.andDeleteStatusEqualTo(Short.parseShort(String.valueOf(StatusCodeEnum.NORMAL_CODE.getState())));
        } else {
            finishOrderProductNum = finishOrderProductNum.substring(0, finishOrderProductNum.length() - 3);
            criteria.andFinishOrderProductNumEqualTo(finishOrderProductNum);
        }
        List<CustomerProductDetailRechargeCode> list = customerProductDetailRechargeCodeMapper.selectByExample(customerProductDetailRechargeCodeExample);
        return list;
    }

    /**
     * 查询优惠劵通过订单id和产品号
     *
     * @param orderId               订单id
     * @param finishOrderProductNum 产品号
     * @return
     */
    @Override
    public List<CustomerProductDetailCoupon> getCouponsByOrderIdAndCph(String orderId, String finishOrderProductNum) {
        CustomerProductDetailCouponExample customerProductDetailCouponExample = new CustomerProductDetailCouponExample();
        CustomerProductDetailCouponExample.Criteria criteria = customerProductDetailCouponExample.createCriteria();
        customerProductDetailCouponExample.setOrderByClause("create_tm asc");
        criteria.andProductOrderIdEqualTo(orderId);

        if (!finishOrderProductNum.contains("CCC")) {
            criteria.andFinishOrderProductNumEqualTo(finishOrderProductNum);
            criteria.andDeleteStatusEqualTo(Short.parseShort(String.valueOf(StatusCodeEnum.NORMAL_CODE.getState())));
        } else {
            finishOrderProductNum = finishOrderProductNum.substring(0, finishOrderProductNum.length() - 3);
            criteria.andFinishOrderProductNumEqualTo(finishOrderProductNum);
        }
        List<CustomerProductDetailCoupon> list = customerProductDetailCouponMapper.selectByExample(customerProductDetailCouponExample);
        return list;
    }

    /**
     * 获取客户下对应的联系人
     *
     * @param customerId 客户Id
     * @return {@link {java.util.List<com.crm.model.entity.customerManager.CustomerContactPerson>}}
     * @Author pym
     * @Date 2021/8/6 9:05
     */
    @Override
    public List<CustomerContactPerson> getCustomerContactPersonByCustomerId(String customerId) {
        CustomerContactPersonExample customerContactPersonExample = new CustomerContactPersonExample();
        CustomerContactPersonExample.Criteria criteria = customerContactPersonExample.createCriteria();
        criteria.andCustomerIdEqualTo(customerId);
        criteria.andDeleteStatusEqualTo(Short.parseShort(String.valueOf(StatusCodeEnum.NORMAL_CODE.getState())));
        List<CustomerContactPerson> list = customerContactPersonMapper.selectByExample(customerContactPersonExample);
        return list;
    }

    /**
     * 俺的客户列表
     *
     * @param customerQO
     * @param order
     * @return
     */
    @Override
    public List<CustomerVO> selectMeCustomerList(CustomerQO customerQO, String order) {
        return customerCustomMapper.selectMeCustomerList(customerQO, order);
    }

    /**
     * selectCustomerMemberCardDetailByProductOrderId
     *
     * @param productOrderId        订单id
     * @param finishOrderProductNum 产品号
     * @return
     * @Author wangpeng
     * @Date 2021/9/16
     */
    @Override
    public CustomerMemberCardDetailVO selectCustomerMemberCardDetailByProductOrderId(String productOrderId, String finishOrderProductNum) {
        return customerCustomMapper.selectCustomerMemberCardDetailByProductOrderId(productOrderId, finishOrderProductNum);
    }

    /**
     * updatechsCustomerMemberCardDetail
     *
     * @param hyhsycs                           行业会次数
     * @param xcch                              现场天数
     * @param accountId                         更新用户id
     * @param memberCardNumber1                 会员卡卡号
     * @param customerProductDetailMemberCardId 会员卡id
     * @return
     * @Author wangpeng
     * @Date 2021/9/16
     */
    @Override
    public void updatechsCustomerMemberCardDetail(String hyhsycs, String xcch, String accountId, String memberCardNumber1, String customerProductDetailMemberCardId) {
        customerCustomMapper.updatechsCustomerMemberCardDetail(hyhsycs, xcch, accountId, memberCardNumber1, customerProductDetailMemberCardId);
    }


    /**
     * addCustomerChangeCourseRecord
     *
     * @param vo
     * @return
     * @Author wangpeng
     * @Date 2021/9/22
     */
    @Override
    public void addCustomerChangeCourseRecord(CustomerChangeCourseRecordVO vo) {
        customerCustomMapper.addCustomerChangeCourseRecord(vo);
    }


    /**
     * 内训部-客户公海
     *
     * @param customerQO
     * @param sortOrder
     * @return
     * @Author renshaorong
     * @Date 2021/9/6
     */
    @Override
    public List<CustomerVO> selectCustomerVOListForInT(CustomerQO customerQO, String sortOrder) {
        return customerCustomMapper.selectCustomerVOListForInT(customerQO, sortOrder);
    }

    /**
     * 内训部-客户公海
     *
     * @param customerQO
     * @param ownerId
     * @param sortOrder
     * @return
     * @Author renshaorong
     * @Date 2021/9/6
     */
    @Override
    public List<CustomerVO> selectCustomerVOListByOwner(CustomerQO customerQO, String ownerId, Integer dateUnit, String sortOrder) {
        return customerCustomMapper.selectCustomerVOListByOwner(customerQO, ownerId, dateUnit, sortOrder);
    }

    /**
     * addFinishOrderProduct
     *
     * @param f
     * @return
     * @Author wangpeng
     * @Date 2021/9/26
     */
    @Override
    public void addFinishOrderProduct(FinishOrderProductVO f) {
        customerCustomMapper.addFinishOrderProduct(f);
    }

    /**
     * selectFinishOrderProductById
     *
     * @param finishOrderProductId
     * @return
     * @Author wangpeng
     * @Date 2021/9/26
     */
    @Override
    public FinishOrderProductVO selectFinishOrderProductById(String finishOrderProductId) {
        return customerCustomMapper.selectFinishOrderProductById(finishOrderProductId);
    }

    @Override
    public String selectCustomerIdByName(String customerName) {
        return customerCustomMapper.selectCustomerIdByName(customerName);
    }

    /**
     * 获取内训下的客户
     *
     * @return
     */
    @Override
    public List<CustomerVO> getCustomerUserList(String accountId) {
        return customerCustomMapper.getCustomerUserList(accountId);
    }

    /**
     * 通过客户名称查询客户
     *
     * @param customerName 客户名称
     * @return
     */
    @Override
    public Customer selectByCustomerName(String customerName) {
        CustomerExample customerExample = new CustomerExample();
        CustomerExample.Criteria criteria = customerExample.createCriteria();
        criteria.andCustomerNameEqualTo(customerName);
        criteria.andDeleteStatusEqualTo(Short.parseShort(String.valueOf(StatusCodeEnum.NORMAL_CODE.getState())));
        List<Customer> customerList = customerMapper.selectByExample(customerExample);
        if (customerList != null && customerList.size() > 0) {
            Customer customer = customerList.get(0);
            return customer;
        } else {
            return null;
        }
    }

    @Override
    public CustomerChangeCourseRecordVO selectChangeCourseCecord(String memberCardNumber) {
        return customerCustomMapper.selectChangeCourseCecord(memberCardNumber);
    }

    @Override
    public List<CustomerChangeCourseRecordVO> selectChangeCourseCecordList(String memberCardNumber) {
        return customerCustomMapper.selectChangeCourseCecordList(memberCardNumber);
    }

    /**
     * 客户详情会员卡
     *
     * @return
     */
    @Override
    public List<CardVO> selectCardByCustomerIdList(String customerId) {
        return customerCustomMapper.selectCardByCustomerIdList(customerId);
    }


    /**
     * 会员成交产品
     */
    @Override
    public List<CustomerAnalysisQo> memberTransactionProducts(CustomerAnalysisRequestQo qo) {
        return customerCustomMapper.selectMemberTransactionProducts(qo);
    }

    /**
     * 非会员成交产品
     */
    @Override
    public List<CustomerAnalysisQo> nonMemberTransactionProducts(CustomerAnalysisRequestQo qo) {
        return customerCustomMapper.selectNonMemberTransactionProducts(qo);
    }

    /**
     * 新增会员
     */
    @Override
    public List<CustomerAnalysisQo> newMember(CustomerAnalysisRequestQo qo) {
        return customerCustomMapper.selectNewMember(qo);
    }

    /**
     * 新增会员
     */
    @Override
    public List<CustomerAnalysisQo> renewMember(CustomerAnalysisRequestQo qo) {
        return customerCustomMapper.selectRenewMember(qo);
    }

    /**
     * 新增会员
     */
    @Override
    public List<CustomerAnalysisQo> expiredMember(CustomerAnalysisRequestQo qo) {
        return customerCustomMapper.selectExpiredMember(qo);
    }
    /**
     * 新增会员
     */
    @Override
    public List<Long> everyDayDealMember(String companyId, String userId, String contractType) {
        return customerCustomMapper.selectEveryDayDealMember(companyId,userId ,contractType);
    }

    /**
     * 客户详情税法直通车
     * @param customerId
     * @return
     */
    @Override
    public List<TaxCardVO> selectTaxCardByCustomerIdList(String customerId) {
        return customerCustomMapper.selectTaxCardByCustomerIdList(customerId);
    }

    /**
     * 通过卡号查询
     * @param memberCardNumber
     * @return
     */
    @Override
    public List<CustomerProductDetailThroughTrainTax> selectTaxNumber(String memberCardNumber) {
        CustomerProductDetailThroughTrainTaxExample customerProductDetailThroughTrainTaxExample = new CustomerProductDetailThroughTrainTaxExample();
        CustomerProductDetailThroughTrainTaxExample.Criteria criteria = customerProductDetailThroughTrainTaxExample.createCriteria();
        criteria.andMemberCardNumberEqualTo(memberCardNumber);
        List<CustomerProductDetailThroughTrainTax> list = customerProductDetailThroughTrainTaxMapper.selectByExample(customerProductDetailThroughTrainTaxExample);
        return list;
    }

    /**
     * 修改税法直通车
     * @param tra
     * @return
     */
    @Override
    public Boolean updateCustomerProductDetailThroughTrainTax(CustomerProductDetailThroughTrainTax tra) {
        return customerProductDetailThroughTrainTaxMapper.updateByPrimaryKeySelective(tra) > 0;
    }

    /**
     * 查询客户会员开始时间和结束时间
     * @return
     */
    @Override
    public List<CustomerMemberDateVO> selectCustomerMemberDate() {
        return customerCustomMapper.selectCustomerMemberDate();
    }

    /**
     * 修改合同状态  改为已结束
     */
    @Override
    public List<CustomerMemberDateVO> selectContractStatus() {
        return customerCustomMapper.selectContractStatus();
    }


    @Override
    public int selectCountById(String id) {
        return customerMapper.selectCountById(id);
    }

    /**
     * 根据公司名、统一信用代码校验数据
     * @param name
     * @param code
     * @return
     */
    @Override
    public int selectCountByNameByCode(String name, String code) {
        return customerMapper.selectCountByNameByCode(name,code);
    }

    /**
     * 用户端 - 客户公海 - 分公司 筛选列表
     * @Author pym
     * @Date  2021/11/23 11:26
     * @param
     * @return {@link {java.util.List<com.crm.model.vo.customerManager.CustomerPoolNameVO>}}
     */
    @Override
    public List<CustomerPoolNameVO> selectCustomerPoolNameList() {
        return customerCustomMapper.selectCustomerPoolNameList();
    }

    /**
     * 用户端 - 客户公海 - 地区 筛选列表
     * @Author pym
     * @Date  2021/11/23 11:40
     * @param customerPoolId -客户池Id
     * @return {@link {java.util.List<java.lang.String>}}
     */
    @Override
    public List<String> selectProvinceNameList(String customerPoolId) {
        return customerCustomMapper.selectProvinceNameList(customerPoolId);
    }

    /**
     * 用户端 - 客户公海 - 标签 筛选列表
     * @Author pym
     * @Date  2021/11/23 14:22
     * @param customerPoolId -客户池Id
     * @return {@link {java.util.List<java.lang.String>}}
     */
    @Override
    public List<String> selectTagList(String customerPoolId) {
        return customerCustomMapper.selectTagList(customerPoolId);
    }

    /**
     * 用户端 - 客户公海 - 客户池下客户列表
     * @Author pym
     * @Date  2021/11/23 15:43
     * @param poolCustomerQO -筛选条件
     * @param sortOrder -排序
     * @return {@link {java.util.List<com.crm.model.vo.customerManager.PoolCustomerVO>}}
     */
    @Override
    public List<PoolCustomerVO> selectPoolCustomerList(PoolCustomerQO poolCustomerQO, String sortOrder) {
        return customerCustomMapper.selectPoolCustomerList(poolCustomerQO, sortOrder);
    }

    /**
     * 用户端 - 客户公海 - 行业 筛选列表
     * @Author pym
     * @Date  2021/12/10 11:03
     * @param customerPoolId - 客户池Id
     * @return {@link {java.util.List<java.lang.String>}}
     */
    @Override
    public List<String> selectIndustryList(String customerPoolId) {
        return customerCustomMapper.selectIndustryList(customerPoolId);
    }

    /**
     * 通过充值卡卡号查询
     * @param card
     * @return
     */
    @Override
    public CustomerProductDetailRechargeCode selectRechargeCode(String card) {
        CustomerProductDetailRechargeCodeExample customerProductDetailRechargeCodeExample = new CustomerProductDetailRechargeCodeExample();
        CustomerProductDetailRechargeCodeExample.Criteria criteria = customerProductDetailRechargeCodeExample.createCriteria();
        criteria.andRechargeCodeCrmIdEqualTo(card);
        criteria.andDeleteStatusEqualTo(Short.parseShort(String.valueOf(StatusCodeEnum.NORMAL_CODE.getState())));
        List<CustomerProductDetailRechargeCode> list = customerProductDetailRechargeCodeMapper.selectByExample(customerProductDetailRechargeCodeExample);
        if (list != null && list.size() > 0) {
            return list.get(0);
        }else{
            return null;
        }

    }

    /**
     * 修改充值卡
     * @param rechargeCode
     */
    @Override
    public Boolean updateRechargeCode(CustomerProductDetailRechargeCode rechargeCode) {
        return customerProductDetailRechargeCodeMapper.updateByPrimaryKeySelective(rechargeCode) > 0;
    }

    /**
     * 修改会员卡
     * @param card
     */
    @Override
    public Boolean updateCustomerProductDetailMemberCard(CustomerProductDetailMemberCard card) {
        return customerProductDetailMemberCardMapper.updateByPrimaryKeySelective(card) > 0;
    }

    /**
     * 通过兑换码查询
     * @param card
     * @return
     */
    @Override
    public CustomerProductDetailExchangeCode selectExchangeCode(String card) {
        CustomerProductDetailExchangeCodeExample customerProductDetailExchangeCodeExample = new CustomerProductDetailExchangeCodeExample();
        CustomerProductDetailExchangeCodeExample.Criteria criteria = customerProductDetailExchangeCodeExample.createCriteria();
        criteria.andExchangeCodeCrmIdEqualTo(card);
        criteria.andDeleteStatusEqualTo(Short.parseShort(String.valueOf(StatusCodeEnum.NORMAL_CODE.getState())));
        List<CustomerProductDetailExchangeCode> list = customerProductDetailExchangeCodeMapper.selectByExample(customerProductDetailExchangeCodeExample);
        if (list != null && list.size() > 0) {
            return list.get(0);
        }else{
            return null;
        }
    }

    /**
     * 修改兑换码
     * @param exchangeCode
     */
    @Override
    public Boolean updateExchangeCode(CustomerProductDetailExchangeCode exchangeCode) {
        return customerProductDetailExchangeCodeMapper.updateByPrimaryKeySelective(exchangeCode) > 0;
    }

    /**
     * 查询会员过期时间小于现在的
     * @param date
     * @return
     */
    @Override
    public List<CustomerVO> selectMembershipExpirationTm(Date date) {
        return customerCustomMapper.selectMembershipExpirationTm(date);
    }

    /**
     * 查询生效中会员合同最大到期时间
     * @return
     */
    @Override
    public List<CustomerContractVO> selectContractEndDate() {
        return customerCustomMapper.selectContractEndDate();
    }

    /**
     * 修改客户的会员等级和流失客户
     * @param customer
     * @return
     */
    @Override
    public Boolean updateCustomerByIdSql(Customer customer) {
        return customerCustomMapper.updateCustomerByIdSql(customer);
    }

    /**
     * 通过卡号查找
     * @param cardNumber
     * @return
     */
    @Override
    public CustomerProductDetailMemberCard selectCardByNumber(String cardNumber) {
        CustomerProductDetailMemberCardExample customerProductDetailMemberCardExample = new CustomerProductDetailMemberCardExample();
        CustomerProductDetailMemberCardExample.Criteria criteria = customerProductDetailMemberCardExample.createCriteria();
        criteria.andMemberCardNumberEqualTo(cardNumber);
        List<CustomerProductDetailMemberCard> list = customerProductDetailMemberCardMapper.selectByExample(customerProductDetailMemberCardExample);
        if (list != null && list.size() > 0) {
            return list.get(0);
        } else {
            return new CustomerProductDetailMemberCard();
        }
    }

    /**
     * 通过客户编号查询
     * @param customerNumber
     * @return
     */
    @Override
    public Customer selectByCustomerNumber(String customerNumber) {
        CustomerExample customerExample = new CustomerExample();
        CustomerExample.Criteria criteria = customerExample.createCriteria();
        criteria.andCustomerNumberEqualTo(customerNumber);
        //criteria.andDeleteStatusEqualTo(Short.parseShort(String.valueOf(StatusCodeEnum.NORMAL_CODE.getState())));
        List<Customer> customerList = customerMapper.selectByExample(customerExample);
        if (customerList != null && customerList.size() > 0) {
            Customer customer = customerList.get(0);
            return customer;
        } else {
            return null;
        }
    }

    /**
     * 客户调转-客户流转列表
     * @param customerName 客户名称
     * @return
     */
    @Override
    public List<CustomerTransferRecordVo> selectCustomerTransferRecordList(String customerName) {
        return customerMapper.selectCustomerTransferRecordList(customerName);
    }

}
